package com.gcsoftware.utils.elasticsearch;

import com.gcsoftware.exception.ElasticsearchException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.gcsoftware.configs.AppConstants.Sys.SYS_MEASURE_DAY;
import static com.gcsoftware.configs.AppConstants.Sys.SYS_MEASURE_SECOND;

/**
 * 时间处理工具类
 * @author czp
 * date : 2018-04-10
 */
public class TimeUtils {
    private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 日志生成时间转化为页面显示时间
     * @param date 日志生成时间
     * @return 格式化时间
     */
    public static String timeFormatting(Date date) {
        return sdf1.format(date);
    }

    /**
     * 将时间转化为long值
     * @param strTime 时间字符串
     * @return long
     */
    public static long strTimeToLong(String strTime, String measure) {
        try {
            Date date;
            if (measure.equals(SYS_MEASURE_SECOND.getValue())) {
                date = sdf1.parse(strTime);
            } else if (measure.equals(SYS_MEASURE_DAY.getValue())) {
                date = sdf2.parse(strTime);
            } else {
                return 0;
            }
            return date.getTime();
        } catch (ParseException e) {
            throw new ElasticsearchException(" strTimeToLong() : " + e.toString());
        }
    }
}
